package com.itogis.interceptors.autoExportExcel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


/**
 * 
 * 导出Excel内容Servlet支持<br>
 * 第一步:在WEB_INF/web.xml文件中注册当前Servlet<br>
 * <textarea rows='5' cols='70'> <servlet> <servlet-name>ExportExcel</servlet-name>
 * <servlet-class>com.iss.common.excel.ExportExcelServlet</servlet-class>
 * </servlet> <servlet-mapping> <servlet-name>ExportExcel</servlet-name>
 * <url-pattern>/ExportExcel</url-pattern> </servlet-mapping> </textarea> <br>
 * 第二步:生成XML数据文件支持<br>
 * <textarea rows='5' cols='70'> <?xml version="1.0" encoding="UTF-8"?>
 * <exportexcel> <excelname>出国历史信息列表</excelname> <property name="ryXm">
 * <propertyname>出国人员姓名</propertyname> </property> <property name="cgLxdBh">
 * <propertyname>出国立项单编号</propertyname> </property> </exportexcel> </textarea>
 * <br>
 * 第三步:业务类中生成exportExcelEntity,将XML文件路径和导出的实体数据放到变量中。<br>
 * 第四步:并exportExcelEntity放到request属性中, 属性名称请参考枚举
 * com.iss.enums.EnRequestAttributeNames.excelExportObjectName<br>
 * 第五步:在struts-config.xml中配置全局forwards<br>
 * <textarea rows='5' cols='70'> <global-forwards> <forward
 * name="common_excel_export" path="/ExportExcel" /> </global-forwards>
 * </textarea> <br>
 * 第六步:在业务JSP中，通过调用全局forward名 common_excel_export 调用当前servlet生成excel<br>
 * 
 * @author Administrator
 * 
 */
public class ExportExcelServlet extends HttpServlet {
	
	/** 类名称 * */

	public static final String className = ExportExcelServlet.class.getName();
	private static final long serialVersionUID = 1L;// 序列化版本UID


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//response.sendRedirect("http://127.0.0.1:12061/TIMProjWeb/imprest/template.xls");
		String outPath="c:\\";
		System.out.println("***********************  a 1");
		ExcelReportManage manage=new ExcelReportManage();
		System.out.println("***********************  a 2");
		String outFilePath="";
		String outFileName="";
		System.out.println("***********************  a 3");
		try {
			outFileName=outFilePath=manage.exportExcelReportByDemo(outPath,"test.xls", 15, 20, "dccsExcel", "");
			System.out.println("***********************  a 4");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("***********************  a 5");
			e.printStackTrace();
		}
		outFilePath=outPath+outFileName;
		System.out.println("***********************  a 6");
		
		System.out.println("***********************  1");
		String fileName=outFileName;
		File file = new File(outFilePath);
		System.out.println("***********************  2");
		request.getParameter("");
		try {
			response.reset();//写文件头
			response.setContentType("application/octet-stream");
			//fileName = new String(fileName.getBytes("gb2312"), "ISO8859_1");
			//response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls" + "\"");
			String exportFileName="attachment;filename=aaaa.xls";			
			response.setHeader("Content-disposition",exportFileName.replaceAll("aaaa.xls", fileName));   
			response.setContentType("bin;charset=gb2312");
			System.out.println("***********************  3");
			int len = (int)file.length();
			byte []buf = new byte[len];
			System.out.println("***********************  4");
			FileInputStream fis = new FileInputStream(file);
			System.out.println("***********************  5");
			OutputStream out = response.getOutputStream();
			System.out.println("***********************  6");
			len = fis.read(buf);
			System.out.println("***********************  7");
			out.write(buf, 0, len);
			System.out.println("***********************  8");
			out.flush();
			System.out.println("***********************  9");
			fis.close();
			System.out.println("***********************  10");
			//file.delete();
			System.out.println("***********************  11");
		} catch (Exception ex) {
			System.out.println("***********************  12xxxxxxxxxxxxx");
			ex.printStackTrace();

		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
